package backpage.service.Impl;

import backpage.entity.Department;
import backpage.mapper.DepartmentMapper;
import backpage.service.DepartmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 查询全部部门信息
     * @return 所有部门列表
     */
    @Override
    public List<Department> queryAllDeptInfo() {
        return departmentMapper.selectList(null);
    }

    /**
     * 根据ID获取部门信息
     * @param departmentId 部门ID
     * @return 部门信息
     */
    @Override
    public Department queryDeptInfoById(Integer departmentId) {
        return departmentMapper.selectById(departmentId);
    }

    /**
     * 新增部门信息
     * @param department 部门对象
     * @return 是否插入成功
     */
    @Override
    public boolean insertDeptInfo(Department department) {
        int insert = departmentMapper.insert(department);
        return insert == 1;
    }

    /**
     * 检查部门编号是否存在
     * @param departmentCode 部门编号
     * @return 如果部门编号唯一，返回true；否则返回false
     */
    @Override
    public boolean checkDeptCodeUnique(String departmentCode) {
        return departmentMapper.selectOne(
                new LambdaQueryWrapper<Department>()
                        .select(Department::getDepartmentId)
                        .eq(Department::getDepartmentCode, departmentCode)
        ) != null;
    }

    /**
     * 检查部门名称是否存在
     * @param departmentName 部门名称
     * @return 如果部门名称唯一，返回true；否则返回false
     */
    @Override
    public boolean checkDeptNameUnique(String departmentName) {
        return departmentMapper.selectOne(
                new LambdaQueryWrapper<Department>()
                        .select(Department::getDepartmentId)
                        .eq(Department::getDepartmentName, departmentName)
        ) != null;
    }

    /**
     * 更新部门信息
     * @param department 部门对象
     * @return 是否更新成功
     */
    @Override
    public boolean updateDeptInfo(Department department) {
        // 参数校验
        if (department == null || department.getDepartmentId() == null) {
            throw new IllegalArgumentException("部门信息或部门ID不能为空");
        }

        // 检查部门是否存在
        Department existingDept = departmentMapper.selectById(department.getDepartmentId());
        if (existingDept == null) {
            throw new RuntimeException("部门不存在，无法更新");
        }

        // 检查部门名称是否重复（如果修改了部门名称）
        if (!existingDept.getDepartmentName().equals(department.getDepartmentName())) {
            boolean isNameUnique = checkDeptNameUnique(department.getDepartmentName());
            if (!isNameUnique) {
                throw new RuntimeException("部门名称已存在");
            }
        }

        // 执行更新
        return departmentMapper.updateById(department) > 0;
    }

    /**
     * 根据经理ID获取部门信息
     * @param managerId 经理ID
     * @return 部门信息
     */
    @Override
    public Department getDepartmentByManagerId(Integer managerId) {
        return departmentMapper.selectOne(
                new LambdaQueryWrapper<Department>()
                        .eq(Department::getManagerId, managerId)
        );
    }

    @Override
    public IPage<Department> queryDeptByPage(Integer pageNum, Integer pageSize) {
        // 创建分页对象
        Page<Department> page = new Page<>(pageNum, pageSize);

        // 可以添加查询条件（如果需要）
        // QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        // queryWrapper.orderByAsc("department_name"); // 按部门名称排序

        // 执行分页查询
        return departmentMapper.selectPage(page, null);
    }


}
